let arr = [];
let data_list;
let app = getApp();
let url = app.globalData.url;
let shuju;
let userId;
let that = this;
let merchantId;
let storeId;
let qrcodeId;
let bkg = "";
let key;
let yu_money;
let openId;
let merchantName;
let totalAmount;
const defHeaders = {
  'Content-Type': 'application/json;charset=UTF-8',
  'Accept': 'application/json',
};
Page({
  data: {
    first: "0",
    key_dis: "",
    membersavings_dis: "",
    all: "",
    money: "",
    yu: "",
    dis: "none",
    blc: "",
    disabled: true,
    box_color: "",
    bkg_color: "",
    key: "",
    index: "",
    recharge_dis: "",
    member_dis: "",
    deposit: "",
    displays: "",
    yuan: "",
    show_index: "",
    current: "0",
    scroll: "",
    scrollleft: "",
    img_s: "",
    border_s: "",
    border_a: "",
    img_a: "",
    dsp: "none",
    dsp_2: "none",
    load:"none",
    cun_dis:""
  },
  list3: [],
  merchantName: '',
  bindKeyOne: function (event) {
    var data_list;
    arr = arr.concat(event.target.dataset.first);
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeyTwo: function (event) {
    var data_list;
    arr = arr.concat(event.target.dataset.two);
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeyThree: function (event) {
    arr = arr.concat(event.target.dataset.three);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeyFour: function (event) {
    arr = arr.concat(event.target.dataset.four);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeyFive: function (event) {
    arr = arr.concat(event.target.dataset.five);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeySix: function (event) {
    arr = arr.concat(event.target.dataset.six);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeySeven: function (event) {
    arr = arr.concat(event.target.dataset.seven);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeyEight: function (event) {
    arr = arr.concat(event.target.dataset.eight);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeyNine: function (event) {
    arr = arr.concat(event.target.dataset.nine);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      //字符串总长度减去小数点位置，再减去1，就是小数位数
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
        this.setData({
          first: data_list,
          disabled: false
        });
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          data_list = parseFloat(data_list).toFixed(2)
          this.setData({
            first: data_list,
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 < 2) {
          this.setData({
            first: data_list,
          });

        }

      }
    }
  },
  bindKeyZero: function (event) {
    arr = arr.concat(event.target.dataset.zero);
    if (arr == false) {
      arr = arr.concat(".");
      var data_list;
      for (var i = 0; i < arr.length; i++) {
        if (arr.length > 0) {
          for (var i = 0; i < arr.length; i++) {
            data_list += arr[i];
          }
        }
        if (data_list.indexOf(".") == -1) {
          data_list = data_list.replace('undefined', '');
        } else {
          data_list = data_list.replace('undefined', '');
          var bitPos = data_list.indexOf(".");
          if (data_list.length - bitPos - 1 == 2) {
            this.setData({
              no_btn: true,
              disabled: true
            });

          }
          if (data_list.length - bitPos - 1 > 2) {
            this.setData({
              no_btn: true,
              disabled: true
            });
            data_list = parseFloat(data_list).toFixed(2)
          }
          //  data_list=parseFloat(data_list).toFixed(2)
        }
      }
      this.setData({
        first: data_list,
        disabled: true,
      });
    } else {
      var data_list;
      for (var i = 0; i < arr.length; i++) {
        if (arr.length > 0) {
          for (var i = 0; i < arr.length; i++) {
            data_list += arr[i];
          }
        }
        if (data_list.indexOf(".") == -1) {
          data_list = data_list.replace('undefined', '');
        } else {
          data_list = data_list.replace('undefined', '');
          var bitPos = data_list.indexOf(".");
          if (data_list.length - bitPos - 1 == 2) {
            this.setData({
              no_btn: true,
              disabled: true
            });

          }
          if (data_list.length - bitPos - 1 > 2) {
            this.setData({
              no_btn: true,
              disabled: true
            });
            data_list = parseFloat(data_list).toFixed(2)
          }
          //  data_list=parseFloat(data_list).toFixed(2)
        }
      }
      this.setData({
        first: data_list,
      });
    }
  },
  bindKeyDian: function (event) {
    arr = arr.concat(event.target.dataset.dian);
    var data_list;
    for (var i = 0; i < arr.length; i++) {
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }
      if (data_list.indexOf(".") == -1) {
        data_list = data_list.replace('undefined', '');
      } else {
        data_list = data_list.replace('undefined', '');
        var bitPos = data_list.indexOf(".");
        if (data_list.length - bitPos - 1 == 2) {
          this.setData({
            no_btn: true,
            disabled: true
          });

        }
        if (data_list.length - bitPos - 1 > 2) {
          this.setData({
            no_btn: true,
            disabled: true
          });
          data_list = parseFloat(data_list).toFixed(2)
        }
        //  data_list=parseFloat(data_list).toFixed(2)
      }
    }
    this.setData({
      first: data_list,
      disabled: true,
    });
  },
  bindKeyInput(event) {
    arr = arr.splice();
    this.setData({
      first: arr,
      disabled: true,
      dis: true,
      first: "0",
      no_btn: false
    });
  },
  onLoad(query) {
      //   判断使用过吗
    console.log("oppId")
    console.log(query)
    yu_money=""
    var that = this;
    // my.showLoading({
    //   content: '加载中...',
    //   delay: 0,
    //   success: () => {
    //   }
    // })
    // 获得页面的userId
    var that = this;
    try {

      wx.getStorage({
        key: 'storeId',
        success: function (res) {
          storeId = res.data.storeId;
          that.merchantName(storeId);
          // my.alert({title:storeId});

        },
        fail: function (res) {
          // my.alert({ concat: "获得storeId失败" });
        }
      });
      wx.getStorage({
        key: 'merchantId',
        success: function (res) {
          merchantId = res.data.merchantId;
          //  my.alert({title:merchantId});
        },
        fail: function (res) {
          // my.alert({ concat: "获得merchantId失败" });
        }
      });
      wx.getStorage({
        key: 'qrcodeId',
        success: function (res) {
          qrcodeId = res.data.qrcodeId;
          //  my.alert({title:merchantId});
        },
        fail: function (res) {
          // my.alert({ concat: "获得merchantId失败" });
        }
      });
    } catch (e) {
      console.log("storeId-------------------GGGGGGGGGGGGG-----------------没存storeId")
      if (merchantName == '' || typeof (merchantName) == "undefined") {
        merchantName = ''
      }
    }

  },
  //获取门店名字
  merchantName(storeId) {
    let merchantName;
    let that = this;
    wx.request({
      url: url + '/store/getByStoreId.do',
      method: 'GET',
      headers: {
        defHeaders,
      },
      data: {
        storeId: storeId,
      },
      dataType: 'json',
      success: function (res) {
        console.log(99999999999);
        console.log(res);
        if (typeof (res.data.body) != "undefined") {
          merchantName = res.data.body.name;
          that.setData({
            merchantName: res.data.body.name,
            merchantc: res.data.body.logo
          });

          wx.setStorage({
            key: 'merchantName',
            data: {
              merchantName: res.data.body.name,
            },
            success: function () {

            }
          });
        } else if (typeof (res.data.body) == "undefined") {

        }

      },
      fail: function (res) {
        console.log(res)
        return '请求商户失败'
      },
      complete: function (res) {
      }
    })


  },
  // 点击确认按钮
  eject: function () {
    var that = this;
    if (arr == false) {
      wx.showToast({
        title: '请输入付款金额',
        duration: 3000,
        mask: true,
        success: function () {
          that.setData({
            load: "none"
          })
          wx.hideLoading();
        }
      })
    } else {
      this.setData({
        load: "block"
      })
      wx.showLoading({
        title: '正在加载',
      })
      // 获得openId，如果存在不在发起授权
      wx.getStorage({
        key: 'openId',
        success(res) {
          if (res.data.openId != "") {
            that.member();
          } else {
            var code
            // 发起授权
            wx.login({
              success(res) {
                console.log("code")
                code = res.code
                console.log(res)
                if (res.code) {
                  // 发起网络请求
                  wx.request({
                    url: url + '/auth/getWxOpenId.do',
                    method: 'GET',
                    data: {
                      auth_code: code,
                        app_id: "wxccca394cfab6aa7d"
                    },
                    headers: {
                      defHeaders,
                    },
                    dataType: 'json',
                    success: function (res) {
                      console.log(6666)
                      openId = res.data.body.openid
                      console.log("获取token: " + JSON.stringify(res))
                      wx.setStorage({
                        key: 'openId',
                        data: {
                          openId: res.data.body.openid,
                          // session_key: res.data.body.session_key,
                        },
                        success: function (res) {
                          // wx.getUserInfo({
                          //   success(res) {
                          //     console.log(999999)
                          //     console.log(res)
                          //  拼接钱
                          that.member();
                          //   }
                          // })
                        }
                      })
                    },
                    fail: function (res) {
                      // wx.showModal({
                      //   content: JSON.stringify(res),
                      // });
                      console.log(res)
                    },
                    complete: function (res) {
                    }
                  })
                } else {
                  console.log('登录失败！' + res.errMsg)
                }
              },
               fail(res) {
                 
                 console.log(res)
               }
            })
          }

        },
        fail(res) {
          var code
          // 发起授权
          wx.login({
            success(res) {
              console.log("code")
              code = res.code
              console.log(res)
              if (res.code) {
                // 发起网络请求
                wx.request({
                  url: url + '/auth/getWxOpenId.do',
                  method: 'GET',
                  data: {
                    auth_code: code,
                      app_id: "wxccca394cfab6aa7d"
                  },
                  headers: {
                    defHeaders,
                  },
                  dataType: 'json',
                  success: function (res) {
                    console.log(6666)
                    openId = res.data.body.openid
                    console.log("获取token: " + JSON.stringify(res))
                    wx.setStorage({
                      key: 'openId',
                      data: {
                        openId: res.data.body.openid,
                        // session_key: res.data.body.session_key,
                      },
                      success: function (res) {
                        // wx.getUserInfo({
                        //   success(res) {
                        //     console.log(999999)
                        //     console.log(res)
                        //  拼接钱
                        that.member();
                        //   }
                        // })
                      }
                    })
                  },
                  fail: function (res) {
                    // wx.showModal({
                    //   content: JSON.stringify(res),
                    // });
                    console.log(res)
                  },
                  complete: function (res) {
                  }
                })
              } else {
                console.log('登录失败！' + res.errMsg)
              }
            },fail(res) {
            
              console.log(res)
            }
          })
        },
      })
    }

  },
  // 判断商户是否有会员
  member: function () {
    var merchantId;
    let that = this;
    wx.getStorage({
      key: 'merchantId',
      success: function (res) {
        merchantId = res.data.merchantId;
        wx.request({
          url: url + '/card/getCardTemplate.do',
          method: 'get',
          headers: {
            'Content-Type': 'application/json;charset=UTF-8',
            'Accept': 'application/json',
          },
          data: {
            merchantId: merchantId,
          },
          dataType: 'json',
          success: function (res) {
            console.log("判断商户是否有会员");
            console.log(res.data.body);
            if (res.data.code == 1) {
              // 商家有会员卡业务(弹出) 

              // 得到余额进行判断 
              wx.getStorage({
                key: 'userId',
                success: function (res) {
                  userId = res.data.userId;
                  wx.request({
                    url: url + '/card/getCard.do',
                    method: 'get',
                    data: {
                      merchantId: merchantId,
                      userId: userId
                    },
                    headers: {
                      defHeaders
                    },
                    dataType: 'json',
                    success: function (res) {
                      if (res.data.body) {
                        yu_money = res.data.body.balance;
                      }
                      //  已经办理会员
                      if (typeof (res.data.body) != "undefined") {
                        //  判断商家设置的值与余额的关系
                        if (res.data.body.depositPointOut > res.data.body.balance) {
                          that.eject_money();
                          that.setData({
                            recharge_dis: "block",
                            member_dis: "none",
                            deposit: "储值优惠买单"
                          })
                        } else if (res.data.body.depositPointOut <= res.data.body.balance || res.data.body.depositPointOut == 0) {
                          var data_list = [];
                          if (arr.length > 0) {
                            for (var i = 0; i < arr.length; i++) {
                              data_list += arr[i];
                            }
                          }
                          data_list = data_list.replace('undefined', '');
                          if (res.data.body.balance > data_list) {
                            //  如果余额大于付款金额，直接付款
                            that.money()
                          } else if (res.data.body.balance <= data_list) {
                            // 如果余额小于等于付款金额弹出   
                            that.eject_money();
                            that.setData({
                              recharge_dis: "block",
                              member_dis: "none",
                              deposit: "储值优惠买单"
                            })
                          }
                        }
                        //  还没有办理会员
                      } else {
                        wx.getStorage({
                          key: 'merchantId',
                          success: function (res) {
                            merchantId = res.data.merchantId;
                            wx.request({
                              url: url + '/card/getWXCard.do',
                              method: 'GET',
                              headers: {
                                defHeaders,
                              },
                              data: {
                                merchantId: merchantId,
                              },
                              dataType: 'json',
                              success: function (res) {
                                // console.log("1111111");
                                // console.log(res.data.body);
                                that.setData({
                                  extradata: res.data.body
                                })
                                that.setData({
                                  recharge_dis: "none",
                                  member_dis: "block",
                                  deposit: "办卡优惠买单"
                                })
                                that.eject_money();
                              },
                              fail: function (res) {
                                console.log(res)
                                return '请求加入会员失败'
                              },
                              complete: function (res) {
                                // my.hideLoading();
                              }
                            })
                          }
                        })
                     
                      }

                    }
                  })
                },

                fail: function (res) {
                  console.log(res)
                  return '请求商户失败'
                },
                complete: function (res) {
                }
              })

            } else {
              // 商家没有会员卡业务（直接付款）
              that.money()
            }

          },

          fail: function (res) {
            console.log(res)
            return '请求商户失败'
          },
          complete: function (res) {
          }
        })
      }
    })
  },
  // 直接付款
  money: function () {
    let that = this;
    //将用户输入金额拼接
    if (arr == false) {
      wx.showToast({
        title: '请输入付款金额',
        duration: 1500,
        mask: true,
        success:function(){
          that.setData({
            load: "none"
          })
          wx.hideLoading();
        }
      })
    } else {
      data_list = [];
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }

      data_list = data_list.replace('undefined', '');
      try {
        if (data_list > 0) {
          wx.getStorage({
            key: 'qrcodeId',
            success: function (res) {
              let qrcodeId = res.data.qrcodeId;
              that.paymentMethod(qrcodeId);//调用支付方法
              // that.setData({
              //   popmoney: data_list
              // })
            },
            fail: function (res) {
              // my.alert({ content: res.errorMessage });
            }
          });
        } else {
          wx.showToast({
            title: '请输入付款金额',
            duration: 1500,
            mask: true,
            success:function(){
              that.setData({
                load: "none"
              })
              wx.hideLoading();
            }
          })
        }
       
      } catch (e) {
        console.log("000scavening-------------------GGGGGGGGGGGGG-----------------没存qrcodeId")
        if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
          qrcodeId = ''
        }
      }
    }
  },
  // 弹出进行付款
  eject_money: function () {
    var that = this;
    // 获得卡券列表
    var getlist3
    //获取卡券列表
    wx.request({
      url: url + "/card/getDepostiList.do",
      method: 'get',
      data: {
        merchantId: merchantId,
        storeId: storeId,

      },
      headers: {
        defHeaders,
      },
      dataType: 'json',
      success: function (res) {
        that.setData({
          load:"none"
        })
        wx.hideLoading();
        console.log(7777777)
        console.log(res)
        if (res.data.body == [] || res.data.body == "") {
          that.money();
          return false;
        }
        // this.handleGetAllAccountInfo();
        shuju = res.data.body;
        getlist3 = res.data.body;
        var jiu = res.data.body;
        var nArr = [];
        var nArrs = [];
        var obj = { "charge_icon": '../../pages/images/charge2.png', "gift_icon": '../../pages/images/gift2.png' }
        for (var i in jiu) {
          if (i < 3) {
            if (jiu[i].benefit == 0) {
              jiu[i].amount1 = jiu[i].amount;
              jiu[i].benefit1 = " ";
              jiu[i].chong1 = " ";

              nArr.push(jiu[i]);
            } else {
              jiu[i].amount1 = jiu[i].amount;
              jiu[i].benefit1 = "赠" + jiu[i].benefit + "元";
              jiu[i].chong1 = "充" + jiu[i].amount + "赠" + jiu[i].benefit;
              nArr.push(jiu[i]);
            }

          } else {
            if (jiu[i].benefit == 0) {
              jiu[i].amount1 = jiu[i].amount;
              jiu[i].benefit1 = " ";
              jiu[i].chong1 = " ";

              nArrs.push(jiu[i]);
            } else {
              jiu[i].amount1 = jiu[i].amount;
              jiu[i].benefit1 = "赠" + jiu[i].benefit + "元";
              jiu[i].chong1 = "充" + jiu[i].amount + "赠" + jiu[i].benefit;
              nArrs.push(jiu[i]);
            }

          }


        }
        if (res.data.body.length <= 3) {
          that.setData({
            list3: nArr,
            list6: nArrs,
            key_dis: "none",
            membersavings_dis: "block",
            dsp: "none",
            dsp_2: "block"
          });
        } else {
          that.setData({
            list3: nArr,
            list6: nArrs,
            key_dis: "none",
            membersavings_dis: "block",
            dsp: "block",
            dsp_2: "none"
          });
        }

        // 让第一个直接显示
        // 得到总得钱数
        console.log("得到列表第一组")
        console.log(res);
        var all = res.data.body[0].amount;
        totalAmount = res.data.body[0].amount;
        var zeng = res.data.body[0].benefit;
        if (zeng == 0) {
          that.setData({
            cun_dis: "none"
          })
        }
        var data_list;
        // 得到要支付的金额
        if (arr == false) {
        } else {
          data_list = [];
          if (arr.length > 0) {
            for (var i = 0; i < arr.length; i++) {
              data_list += arr[i];
            }
          }

          data_list = data_list.replace('undefined', '');
        }
        console.log(data_list);
        var money = data_list;

        var yu = all + zeng - money;
        if (yu < 0) {
          that.setData({
            all: all,
            money: money,
            yu: "不足",
            isSelect: "0",
            yuan: ""
          });
          return false;
        }
        console.log(yu);
        that.setData({
          all: all,
          money: money,
          yu: yu,
          isSelect: "0",
          yuan: "元"
        });
      },
      fail: function (res) {
        // my.alert({
        //   content: res,
        // });
        console.log("获取卡券列表eeeeeeeeeeeeeeeeeeeee" + res)
      },
      complete: function (res) {

      }
    });
  },
  // 弹出框的钱数
  handleItemClick: function (ev) {
    console.log(ev)
    var self = this;
    // 得到总得钱数
    var all = ev.currentTarget.dataset.amount;
    totalAmount = ev.currentTarget.dataset.amount;
    var index = ev.currentTarget.dataset.index;
    var zeng = shuju[index].benefit;
    if (zeng == 0) {
      self.setData({
        cun_dis: "none"
      })
    } else {
      self.setData({
        cun_dis: "block"
      })
    }
    console.log(shuju[index].benefit);
    var data_list;
    // 得到要支付的金额
    if (arr == false) {

    } else {
      data_list = [];
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }

      data_list = data_list.replace('undefined', '');
    }
    console.log(data_list);
    var money = data_list;

    var yu = all + zeng - money;
    if (yu < 0) {
      this.setData({
        all: all,
        money: money,
        yu: "不足",
        bkg: "#c0c0c0 !important",
        isSelect: index,
        yuan: "",
        isSelects: 10
      });
      setTimeout(function () {
        self.setData({
          bkg: ""
        });
      }, 300);
      return false;
    }
    console.log(yu);
    this.setData({
      all: all,
      money: money,
      yu: yu,
      bkg: "#c0c0c0 !important",
      isSelect: index,
      yuan: "元",
      isSelects: 10
    });
    // alert(key);
    setTimeout(function () {
      self.setData({
        bkg: ""
      });
    }, 300);
  },
  // 弹出框的钱数
  handleItem: function (eve) {
    console.log(eve)
    var self = this;
    // 得到总得钱数
    var all = eve.currentTarget.dataset.amount;
    totalAmount = eve.currentTarget.dataset.amount;
    var index = parseInt(eve.currentTarget.dataset.index) + 3;
    var zeng = shuju[index].benefit;
    if (zeng == 0) {
      self.setData({
        cun_dis: "none"
      })
    } else {
      self.setData({
        cun_dis: "block"
      })
    }
    console.log(shuju[index].benefit);
    var data_list;
    // 得到要支付的金额
    if (arr == false) {

    } else {
      data_list = [];
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }

      data_list = data_list.replace('undefined', '');
    }
    console.log(data_list);
    var money = data_list;

    var yu = all + zeng - money;
    if (yu < 0) {
      this.setData({
        all: all,
        money: money,
        yu: "不足",
        bkg: "#c0c0c0 !important",
        isSelects: eve.currentTarget.dataset.index,
        yuan: "",
        isSelect: 10
      });
      setTimeout(function () {
        self.setData({
          bkg: ""
        });
      }, 300);
      return false;
    }
    console.log(yu);
    this.setData({
      all: all,
      money: money,
      yu: yu,
      bkg: "#c0c0c0 !important",
      isSelects: eve.currentTarget.dataset.index,
      yuan: "元",
      isSelect: 10
    });
    // alert(key);
    setTimeout(function () {
      self.setData({
        bkg: ""
      });
    }, 300);
  },
  // 直接的付款
  confirm: function () {
    var that=this;
    if (arr == false) {
      wx.showToast({
        title: '请输入付款金额',
        duration: 3000,
        mask: true,
        success:function(){
          that.setData({
            load: "none"
          })
          wx.hideLoading();
        }
      })

      
    }else{
      this.setData({
        load: "block"
      })
      wx.showLoading({
        title: '正在加载',
      })
      this.money();
    }
  },
  recharge() {
    var that=this;
    wx.getStorage({
      key: 'userId',
      success: function (res) {
        var userId = res.data.userId;
        wx.getStorage({
          key: 'openId',
          success: function (res) {
            var openId = userId + ',' + res.data.openId
            wx.getStorage({
              key: 'storeId',
              success: function (res) {
                var storeId = res.data.storeId;
                // 生成订单号
                wx.request({
                  url: url + '/card/createWxPay.do',
                  method: 'get',
                  headers: {
                    'Content-Type': 'application/json;charset=UTF-8',
                    'Accept': 'application/json',
                  },
                  data: {
                    storeId: storeId,
                    totalAmount: totalAmount,
                    openId: openId,
                  },
                  dataType: 'json',
                  success: function (res) {
                    that.setData({
                      load: "none"
                    })
                    wx.hideLoading()
                    console.log("订单返回")
                    var appid = res.data.body.appId
                    console.log(res.data.body.paySign)
                    var paySign = res.data.body.paySign
                    console.log(res.data.body.nonceStr)
                    var nonceStr = res.data.body.nonceStr
                    console.log(res.data.body.packageStr)
                    var packageStr = res.data.body.packageStr
                    console.log(res.data.body.timeStamp)
                    var timeStamp = res.data.body.timeStamp
                      var signType = res.data.body.signType
                    console.log(res)

                    if (res.data.code == 1) {
                      // wx.request({
                      //   url: url + '/auth/getCxcSign.do',
                      //   method: 'get',
                      //   data: {
                      //     appid: "",
                      //     packageStr: packageStr,
                      //     nonceStr: nonceStr
                      //   },
                      //   headers: {
                      //     'Content-Type': 'application/json;charset=UTF-8',
                      //     'Accept': 'application/json',
                      //   },
                      //   dataType: 'json',
                      //   success: function (res) {
                      //     console.log("签名")
                      //     console.log(res)
                      //4.唤起支付
                      wx.requestPayment({
                        timeStamp: timeStamp,
                        nonceStr: nonceStr,
                        package: packageStr,
                          signType: signType,
                        paySign: paySign,
                        success: (res) => {
                          that.setData({
                            membersavings_dis: "none",
                            key_dis: "block",
                            no_btn: false,
                          })
                            wx.setStorage({
                                key: 'type',
                                data: {
                                    type: 2,
                                },
                            })
                        },
                        fail: (res) => {
                          console.log(res)
                        }
                      })

                      //   }
                      // })



                    } else if (res.data.code == 2) {
                      //  my.alert({
                      //    title:"支付成功",
                      //   content: res.data.body.totalAmount
                      // });
                      that.tanchu();
                    }


                  },
                  fail: function (res) {
                    console.log(res)
                  },
                  complete: function (res) {


                  }
                })
              }
            })
          }
        })
      }
    })


   
  },
  hand: function (eve) {
    console.log(eve)
  },
  //支付方法
  paymentMethod: function (qrcodeId) {
    // let userID = my.getStorageSync({ key: 'userId' }).data.userId;
    // console.log('获取成功：' + userID)
    var that = this;
    //将用户输入金额拼接
    if (arr == false) {
      wx.showToast({
        title: '请输入付款金额',
        duration: 3000,
        mask: true,
        success: function () {
          that.setData({
            load: "none"
          })
          wx.hideLoading();
        }
      })
    } else {
      data_list = [];
      if (arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
          data_list += arr[i];
        }
      }

      data_list = data_list.replace('undefined', '');
      console.log(8589678444)
      console.log(data_list);
    }
    try {
      if (data_list > 0) {
        wx.getStorage({
          key: 'userId',
          success: function (res) {
            var userId = res.data.userId
            wx.request({
              url: url + '/store/getByStoreId.do',
              method: 'GET',
              headers: {
                defHeaders,
              },
              data: {
                storeId: storeId,
              },
              dataType: 'json',
              success: function (res) {
                var subject = res.data.body.name;
                // 如果余额大于待支付金额弹出提示是否付款
                if (yu_money >= data_list) {
                  wx.showModal({
                    title: '储值余额支付',
                    content: `${data_list}` + '元',
                    success(res) {
                      if (res.confirm) {
                        that.pay(qrcodeId, data_list)
                      } else if (res.cancel) {
                        that.setData({
                          load: "none"
                        })
                        wx.hideLoading();
                      }
                    }
                  })
                } else {
                  that.pay(qrcodeId, data_list)
                  
                }
              },
              fail: function (res) {
               
              },
              complete: function (res) {
              }
            })
      }
    })
      } else {
        wx.showToast({
          title: '请输入付款金额',
          duration: 3000,
          mask: true,
          success: function () {
            that.setData({
              load: "none"
            })
            wx.hideLoading();
          }
        })
      }
    } catch (e) {
      console.log("000scavening-------------------GGGGGGGGGGGGG-----------------没存qrcodeId")
      if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
        qrcodeId = ''
      }
    }
  },
  pay: function (qrcodeId, data_list){
    var that=this;
    wx.getStorage({
      key: 'openId',
      success: function (res) {
        console.log('openId')
        console.log(res.data.openId)
        var openId = userId + ',' + res.data.openId;
        console.log(openId)
        // 生成订单号
        wx.request({
          url: url + '/qrcode/createWxQrcodePay.do',
          method: 'get',
          headers: {
            defHeaders,
          },

          data: {
            // qrcodeId: '265106126438244352', //qrcodeId
            qrcodeId: qrcodeId,
            totalAmount: data_list,
            openId: openId,
            type: 1,
            // note: "",
            // aliUserId: userId
          },
          dataType: 'json',
          success: function (res) {
            // 去除等待
            that.setData({
              load: "none"
            })
            wx.hideLoading();
            console.log("订单返回")
            console.log(res)
            var appid = res.data.body.appId
            console.log(res.data.body.paySign)
            var paySign = res.data.body.paySign
            console.log(res.data.body.nonceStr)
            var nonceStr = res.data.body.nonceStr
            console.log(res.data.body.packageStr)
            var packageStr = res.data.body.packageStr
            console.log(res.data.body.timeStamp)
            var timeStamp = res.data.body.timeStamp
              var signType = res.data.body.signType
              

            if (res.data.code == 1) {
              that.setData({
                money: data_list
              })
                  //4.唤起支付
                  wx.requestPayment({
                    timeStamp: timeStamp,
                    nonceStr: nonceStr,
                    package: packageStr,
                      signType: signType,
                    paySign: paySign,
                    success(res) {
                      console.log(res)
                      arr = [];
                      that.setData({
                        data_list: "0",
                        membersavings_dis: "none",
                        key_dis: "block",
                        first: "0",
                        no_btn: false
                      })
                        wx.setStorage({
                            key: 'type',
                            data: {
                                type: 2,
                            },
                        })
                    },
                    fail(res) {
                      console.log(res)
                      wx.showModal({
                        title: '支付提示',
                        content: '支付失败，请重新支付',
                        showCancel: false,
                        success: function (res) {

                        }
                      })
                    }
                  })
            } else if (res.data.code == 2) {
              that.setData({
                popmoney: res.data.body.totalAmount,
              })
              that.tanchu();
            }


          },
          fail: function (res) {
            console.log(res)
          },
          complete: function (res) {


          }
        })
      }
    })
  },
  // defaultTap(){
  // 	my.navigateTo({
  // 											url:"../luckdraw/luckdraw"
  // 										});

  // },
  tanchu: function () {
    this.setData({
      blc: "block",
    })
  },
  pop: function () {
    arr = arr.splice();
    this.setData({
      blc: "none",
      membersavings_dis: "none",
      first: arr,
      key_dis: "block",
      dis: "none",
      disabled: true,
      no_btn:false
    })
  },
  membersavings: function () {
    this.setData({
      key_dis: "block",
      membersavings_dis: "none",
      no_btn:false
    });
  },
  // 会员办理
  member_handle: function () {
    
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    var that = this;
    that.setData({
      membersavings_dis: "none",
      key_dis: "block",
      displays: "none",
      no_btn: false,
      load: "none"
    });
    wx.hideLoading();
    // 是否跳转积分商城

      wx.getStorage({
          key: 'type',
          success: function (res) {
              console.log(res.data.type)
              if (res.data.type == 3) {
                  wx.redirectTo({
                      url: '../recommend/recommend',
                  })
              }
          }
      })

  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
    console.log("&&&&&&&&&&&&&&&-----------支付页面关闭-------------->>>>>>>>>>>>>>")
    // my.clearStorageSync()
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    arr = [];
    var that = this;
    that.setData({
      membersavings_dis: "none",
      key_dis: "block",
      first: "0",
      no_btn: false,
      load: "none"
    }); 
    wx.stopPullDownRefresh()
    wx.hideLoading();
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    return {
      title: '分享获得优惠券',
      path: 'pages/index/index?sharetype=1&merchantId=' + merchantId + '&storeId=' + storeId + '&qrcodeId=' + qrcodeId,
      imageUrl: "../images/1.jpg"
    };
  },
});